package com.creative.xcx.controller;

import com.alibaba.excel.EasyExcel;
import com.creative.core.controller.BaseController;
import com.creative.core.domain.AjaxResult;
import com.creative.xcx.domain.UploadHistory;
import com.creative.xcx.service.UploadHistoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author ling
 * @description 上传记录
 * @date 2025-03-15
 */
@RestController
@RequestMapping(value = "/UploadHistory")
public class UploadHistoryController extends BaseController {

    @Resource
    private UploadHistoryService uploadHistoryService;

    /**
     * 新增
     *
     * @author ling
     * @date 2025/03/15
     **/
    @PostMapping
    public AjaxResult insert(@RequestBody UploadHistory uploadHistory) {
        return toAjax(uploadHistoryService.insert(uploadHistory));
    }

    /**
     * 刪除单个
     *
     * @author ling
     * @date 2025/03/15
     **/
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        return toAjax(uploadHistoryService.delete(id));
    }

    /**
     * 批量刪除
     *
     * @author ling
     * @date 2025/03/15
     **/

    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids) {
        return toAjax(uploadHistoryService.deleteBatch(ids));
    }


    /**
     * 更新
     *
     * @author ling
     * @date 2025/03/15
     **/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody UploadHistory uploadHistory) {
        return toAjax(uploadHistoryService.update(uploadHistory));
    }


    /**
     * 查询 根据主键 id 查询
     *
     * @author ling
     * @date 2025/03/15
     **/
    @GetMapping(value = "/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        if (id == null) {
            return error("参数为空");
        }
        return success(uploadHistoryService.selectById(id));
    }

    /**
     * 查询 分页查询(带条件)
     *
     * @author ling
     * @date 2025/03/15
     **/
    @GetMapping("/list")
    public AjaxResult selectUploadHistoryList(UploadHistory uploadHistory) {

        return success(uploadHistoryService.selectUploadHistoryList(uploadHistory));
    }

    /**
     * 批量导出
     *
     * @author ling
     * @date 2025/03/15
     **/
    @PostMapping("/export")
    public void export(@RequestBody Long[] ids, HttpServletResponse response) throws IOException {
        //        判断ids是否为空
        if (ids == null || ids.length == 0) {
            // ids 为空或未初始化时执行的代码
            throw new RuntimeException("ids为空");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("上传记录" + System.currentTimeMillis(), StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UploadHistory.class)
                .sheet("上传记录")
                .doWrite(() -> {
                    // 查询数据
                    return uploadHistoryService.exportBatch(ids);
                });
        logger.info("导出成功");
    }

    @GetMapping("/statistics/upload")
    public AjaxResult calculateUploadMaterial() {
        return success(uploadHistoryService.calculateUploadMaterial());
    }
}
